package com.csdig.cms.web.admin;

import com.csdig.cms.common.ConstantDefine;
import com.csdig.cms.model.CmsSetting;
import com.csdig.cms.service.SettingService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = ConstantDefine.AdminUrl.BASE + "/setting")
public class SettingController {

	@Autowired
	private SettingService settingService;

	/*@Autowired
	private CacheService cacheSv;*/

	@RequestMapping(value = "list")
	public String list(HttpServletRequest request) throws Exception {
		request.setAttribute("list", settingService.findAll());
		return "setting/list";
	}

	@RequestMapping(value = "add")
	public String add() {
		return "setting/add";
	}

	@RequestMapping(value = "save")
	@ResponseBody
	public Map<String, Object> save(CmsSetting setting, String type) throws Exception {
		if (StringUtils.isNotEmpty(type)) {
			settingService.update(setting);
		} else {
			settingService.add(setting);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		return map;
	}

	@RequestMapping(value = "del")
	@ResponseBody
	public Map<String, Object> del(String name) throws Exception {
		settingService.del(name);
		Map<String, Object> map = new HashMap<String, Object>();
		return map;
	}

	@RequestMapping(value = "clearCache")
	@ResponseBody
	public Map<String, Object> clearCache() throws Exception {
//		cacheSv.clearCache();
		Map<String, Object> map = new HashMap<String, Object>();
		return map;
	}

}
